package com.dongdongshop.controller;

import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("contentCategory")
public class ContentCategoryController {

    @DubboReference
    private ContentCategoryService categoryService;

    //跳转广告类型管理
    @RequestMapping("toContentCategory")
    public String toContentCategory(){
        return "admin/content_category";
    }

    //广告类型查询
    @RequestMapping("contentCategoryInfo")
    @ResponseBody
    public List<TbContentCategory> contentCategoryInfo(){
        List<TbContentCategory> categoryList = categoryService.findAll();
        return categoryList;
    }

    //跳转广告类型添加页面
    @RequestMapping("toContentCategoryAdd")
    public String toContentCategoryAdd(){
        return "content_category/add";
    }

    //广告类型添加
    @RequestMapping("contentCategoryAdd")
    @ResponseBody
    public AjaxResult contentCategoryAdd(TbContentCategory category){
        try {
            int i = categoryService.contentCategoryAdd(category);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //跳转广告类型修改页面
    @RequestMapping("toContentCategoryUpdate")
    public String toContentCategoryUpdate(Long id, Model model){
        TbContentCategory category = categoryService.getCategoryById(id);
        model.addAttribute("category",category);
        return "content_category/update";
    }

    //广告类型修改
    @RequestMapping("contentCategoryUpdate")
    @ResponseBody
    public AjaxResult contentCategoryUpdate(TbContentCategory category){
        try {
            int i = categoryService.contentCategoryUpdate(category);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //广告类型删除
    @RequestMapping("deleteId")
    @ResponseBody
    public AjaxResult deleteId(Long id){
        try {
            int i = categoryService.deleteId(id);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
